
package newcrunch.codeanalysis;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.sourceforge.pmd.PMD.ProgressMonitor;

/**
 *
 * @author WL
 */
public class ProgressChangeListener implements PropertyChangeListener, ProgressMonitor
{
    private JTextField textField;
    private JProgressBar bar;
    
    public ProgressChangeListener(JTextField textField, JProgressBar bar)
    {
        this.textField = textField;
        this.bar = bar;
    }
    
    @Override
    public void propertyChange(final PropertyChangeEvent evt) 
    {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                String name = evt.getPropertyName();
                
                if ("newstatus".equals(name))
                    textField.setText(evt.getNewValue().toString());
                
                else if ("addstatus".equals(name))
                    textField.setText(textField.getText() + 
                                            evt.getNewValue().toString());
                
                else if ("progress".equals(name))
                {
                    bar.setString(null);
                    bar.setValue((Integer)evt.getNewValue());
                }
            }
        });
    }

    @Override
    public void progress(final String statusMsg, final int count, final int total) 
    {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                bar.setString(null);
                bar.setMaximum(total);
                bar.setValue(count);
                textField.setText(statusMsg);                
            }
        });
    }
}
